package com.weiyuwqj.admin.controller.admin.account;

import com.weiyuwqj.admin.controller.admin.log.annotation.LogAnnotation;
import com.weiyuwqj.api.service.BusAccountBankService;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.model.BusAccountBank;
import com.weiyuwqj.api.model.convert.AccountBankAddtimeConvert;
import com.weiyuwqj.api.model.convert.AccountBankStatusConvert;
import com.weiyuwqj.api.model.convert.ModelConvert;
import com.weiyuwqj.api.model.vo.BusAccountBankVo;
import com.weiyuwqj.api.util.ExcelUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * The function of saving, modifying and deleting the site module
 * <p/>
 * Copyright 2015-2019 Yunfucloud technology co., Ltd.
 * Created by hanling on 15/10/24.
 */
@Controller
@RequestMapping("/admin/accountbank")
public class AccountBankController {

    @Autowired
    private BusAccountBankService accountBankService;

    /**
     * 银行卡修改
     *
     * @param busAccountBank
     */
    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @LogAnnotation(module = "账户管理/银行卡管理", description = "修改银行卡")
    public void edit(BusAccountBank busAccountBank) {
	    accountBankService.updateNotNull(busAccountBank);
    }


    /**
     * 显示银行卡信息
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Object list(
            @ModelAttribute Paginator p,
            HttpServletRequest request
    ) {
        String conditionName = request.getParameter("conditionName");
        String cardNo = request.getParameter("cardNo");
        String status = request.getParameter("status");
        String userType = request.getParameter("userType");
        String beginDateTime = request.getParameter("beginDateTime");
        String endDateTime = request.getParameter("endDateTime");
        String orderName = request.getParameter("orderName");
        String orderWay = request.getParameter("orderWay");
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isNotEmpty(conditionName)) {
            map.put("conditionName", conditionName);
        }
        if (StringUtils.isNotEmpty(cardNo)) {
            map.put("cardNo", cardNo);
        }
        if (StringUtils.isNotEmpty(status)) {
            map.put("status", status);
        }
        if (StringUtils.isNotEmpty(beginDateTime)) {
            map.put("beginDateTime", beginDateTime);
        }
        if (StringUtils.isNotEmpty(endDateTime)) {
            map.put("endDateTime", endDateTime);
        }
        if (StringUtils.isNotEmpty(orderName)) {
            map.put("orderName", orderName);
        }
        if (StringUtils.isNotEmpty(orderWay)) {
            map.put("orderWay", orderWay);
        }
        if (StringUtils.isNotEmpty(userType)) {
            map.put("userType", userType);
        }

	    return accountBankService.getList(p, map);
    }

    /**
     * 列出所有银行卡记录
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getSingle", method = RequestMethod.POST)
    public Object getSingle(@RequestParam(defaultValue = "0") Integer id, Integer userId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        map.put("userId", userId);
	    return accountBankService.getSingle(map);
    }

    //根据用户id查询银行卡信息
    @ResponseBody
    @RequestMapping(value = "/getBankByUserId", method = RequestMethod.POST)
    public Object getBankUserById(@RequestParam Integer userId) {
	    List list = accountBankService.getBankByUserId(userId);
	    if (list.size() == 0) {
            return null;
        } else {
            return list;
        }
    }

    /**
     * 用户银行卡导出
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/excel/accountBank")
    public Object accountBankExcel(HttpServletRequest request,
                                   String conditionName,
                                   String cardNo,
                                   String status,
                                   String beginDateTime,
                                   String endDateTime) {

        String[] cArray = {"ID", "用户名", "真实姓名", "手机号码", "银行卡名称", "银行卡号", "省", "市", "县区", "状态", "添加时间"};
        String[] eArray = {"Id", "Username", "Realname", "Mobile", "BankName", "CardNo", "Province", "City", "Area",
                "Status", "Addtime"};
        ModelConvert[] converts = {null, null, null, null, null, null, null, null, null, new AccountBankStatusConvert(),
                new AccountBankAddtimeConvert()};

        List<String> cList = Arrays.asList(cArray);
        List<String> eList = Arrays.asList(eArray);
        List<ModelConvert> convertList = Arrays.asList(converts);
        Map map = new HashMap();
        map.put("conditionName", conditionName);
        map.put("cardNo", cardNo);
        map.put("status", status);
        map.put("beginDateTime", beginDateTime);
        map.put("endDateTime", endDateTime);
	    List<BusAccountBankVo> contentList = accountBankService.getList(map);
	    String serverDir = request.getServletContext().getRealPath("/") + "admin/export/";
        String excelFilePath = ExcelUtils.export("银行卡管理", cList, eList, convertList, contentList, serverDir);
        HashMap ret = new HashMap();
        ret.put("result", "success");
        ret.put("file", "export/" + excelFilePath);
        return ret;
    }

    /**
     * 修改银行卡状态
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateBankStatus", method = RequestMethod.POST)
    @LogAnnotation(module = "账户管理/银行卡管理", description = "修改银行卡状态")
    public void updateBankStatus(Integer newStatus, Integer oldStatus, Integer userId) {
        Map map = new HashMap();
        map.put("newStatus", newStatus);
        map.put("oldStatus", oldStatus);
        map.put("userId", userId);
	    accountBankService.updateBankStatus(map);
    }

    /**
     * 根据卡号查询卡信息
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getOneByCardNo", method = RequestMethod.POST)
    public Object getOneByCardNo(String cardNo) {
	    return accountBankService.getOneByCardNo(cardNo);
    }

    /**
     * 根据userId查询状态为0的卡
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getOneByUserIdAndStatus", method = RequestMethod.POST)
    public Object getOneByUserIdAndStatus(Integer userId, Integer status) {
        Map map = new HashMap();
        map.put("userId", userId);
        map.put("status", status);
	    return accountBankService.getOneByUserIdAndStatus(map);
    }
}
